use sea_orm::ActiveValue::Set;
use sea_orm::{ActiveModelTrait, EntityTrait};

use common::ctx::get_db_by_key;
use common::error::MyError;
use entity::cash_cera;
use entity::prelude::CashCera;
use model::accounts::RechargeBondsModel;

pub struct CachCeraManager {}

impl CachCeraManager {
    pub async fn recharge_bonds(form:RechargeBondsModel) -> Result<bool,MyError> {
        let entity = CashCera::find_by_id(form.uid.to_string())
            .one(&*get_db_by_key("taiwan_billing")?)
            .await?
            .ok_or(MyError::ServerError(format!(
                "cash_cera [{:?}] does not exist",
                form.uid
            )))?;
        let mut entity: cash_cera::ActiveModel = entity.into();
        entity.cera = Set(entity.cera.unwrap() + form.count);
        let _result = entity.update(&*get_db_by_key("taiwan_billing")?).await?;
        Ok(true)
    }
}